var user_id = 1;
module.exports = {
  /**
    * Before connection (optional, just for faye)
    * @param {client} client connection
    */
  beforeConnect: function(client) {
    // Example:
    // client.setHeader('Authorization', 'OAuth abcd-1234');
    // client.disable('websocket');
  },

  /**
    * On client connection (required)
    * @param {client} client connection
    * @param {done} callback function(err) {}
    */
  onConnect: function(client, done) {
    // Faye client
    // client.subscribe('/channel', function(message) { });

    // Socket.io client
    // client.emit('test', { hello: 'world' });

    // Primus client
    // client.write('Sailing the seas of cheese');

    // WAMP session
    // client.subscribe('com.myapp.hello').then(function(args) { });
    client.emit("user-login", {
      wx_code: (user_id++ / 100000).toString().substr(2)
    });
    client.once("user-login-success", () => {
      client.on("player-frame", player => {
        if (player.status === 0) {
          //进入匹配队列
          // client.emit("join-queue", { player_number: 4 });
        } else if (player.status === 11) {
          //等待确认匹配
          console.log("等待确认匹配");
          client.emit("confirm-queue-to-room"); // 确认进入匹配队列
        } else if (player.status === 12) {
          console.log("已经确认进入匹配");
          done();
        }
      });
      console.log("开始排队");
      client.emit("join-queue", { player_number: 4 });
    });
  },

  /**
    * Send a message (required)
    * @param {client} client connection
    * @param {done} callback function(err) {}
    */
  sendMessage: function(client, done) {
    // Example:
    // client.emit('test', { hello: 'world' });
    // client.publish('/test', { hello: 'world' });
    // client.call('com.myapp.add2', [2, 3]).then(function (res) { });

    done();
  },

  /**
    * WAMP connection options
    */
  options: {
    // realm: 'chat'
  }
};
